//package com.hucheng.rest.aspect.logs;
//
//import com.alibaba.fastjson.JSON;
//import com.alibaba.fastjson.serializer.SerializerFeature;
//import com.hucheng.common.response.ServiceCoreVO;
//import com.hucheng.common.result.ReturnVO;
//import com.hucheng.common.utils.ReadPropertiesUtils;
//import lombok.extern.slf4j.Slf4j;
//import org.aspectj.lang.JoinPoint;
//import org.aspectj.lang.ProceedingJoinPoint;
//import org.aspectj.lang.annotation.Around;
//import org.aspectj.lang.annotation.Aspect;
//import org.aspectj.lang.annotation.Before;
//import org.aspectj.lang.annotation.Pointcut;
//import org.springframework.stereotype.Component;
//import org.springframework.web.context.request.RequestAttributes;
//import org.springframework.web.context.request.RequestContextHolder;
//import org.springframework.web.context.request.ServletRequestAttributes;
//
//import javax.servlet.http.HttpServletRequest;
//import java.net.URLDecoder;
//import java.util.Properties;
//
///**
// * @Author: 凉白开不加冰
// * @Version: 0.0.1V
// * @Date: 2019-06-05
// * @Description: 请求日志结果切面
// **/
//@Slf4j
//@Aspect
//@Component
//public class LogRecordAspect {
//
//    private static final Properties properties = ReadPropertiesUtils.getProperties();
//
//    @Pointcut("execution(*  *..*.*.controller..*.*(..))")
//    public void pointcut() {
//    }
//
//
//    @Before("pointcut()")
//    public void beginTransaction(JoinPoint joinPoint) {
//        RequestAttributes ra = RequestContextHolder.getRequestAttributes();
//        ServletRequestAttributes sra = (ServletRequestAttributes) ra;
//        HttpServletRequest request = sra.getRequest();
//        long startTime = System.currentTimeMillis();
//        request.setAttribute("startTime", startTime);
//    }
//
//    @Around("pointcut()")
//    public Object around(ProceedingJoinPoint pjp) throws Throwable {
//        RequestAttributes ra = RequestContextHolder.getRequestAttributes();
//        ServletRequestAttributes sra = (ServletRequestAttributes) ra;
//        HttpServletRequest request = sra.getRequest();
//        String method = request.getMethod();
//        String queryString = request.getQueryString();
//        Object[] args = pjp.getArgs();
//        String params = "";
//
//        String api = request.getRequestURI();
//        String[] class_name = pjp.getTarget().getClass().getName().split("\\.");
//        String method_name = pjp.getSignature().getName();
//        int code = 0;
//
//        // result的值就是被拦截方法的返回值
//        Object result = null;
//        try {
//            result = pjp.proceed();
//        } catch (Exception e) {
//            code = Integer.parseInt(e.getMessage());
//        }
//
//        ServiceCoreVO serviceCoreVO = (ServiceCoreVO) result;
//
//        StringBuffer sb = new StringBuffer();
//        long startTime = (long) request.getAttribute("startTime");
//        long endTime = System.currentTimeMillis();
//
//        sb.append("ip-->" + request.getRemoteAddr());
//        sb.append(";api-->" + api);
//        sb.append(";method-->" + class_name[class_name.length - 1] + "_" + method_name);
//        sb.append(";request_time-->" + request.getAttribute("startTime"));
//        sb.append(";complete_time-->" + endTime);
//        code = serviceCoreVO == null ? code : serviceCoreVO.getCode();
//        sb.append(";code-->" + code);
//        String msg = serviceCoreVO == null ? properties.getProperty(code + "") : serviceCoreVO.getMessage();
//        sb.append(";msg-->" + msg);
//
//        Object data = serviceCoreVO == null ? null : serviceCoreVO.getData();
//        String responseBody = JSON.toJSONString(data);
//        try {
//            //获取请求参数集合并进行遍历拼接
//            if (args.length > 0) {
//                if ("POST".equals(method)) {
//                    Object object = args[0];
//                    params = JSON.toJSONString(object, SerializerFeature.WriteMapNullValue);
//                } else if ("GET".equals(method)) {
//                    params = queryString;
//                }
//                if (params != null) {
//                    params = URLDecoder.decode(params, "UTF-8");
//                }
//            }
//            sb.append(";params-->" + params);
//            sb.append(";responseBody-->" + responseBody);
//            sb.append(";consume-->" + (endTime - startTime) + "ms");
//            log.info("请求参数:" + sb.toString());
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        if (result == null) {
//            return new ServiceCoreVO(code);
//        }
//        return result;
//    }
//
//}
